<template>
  <div>
    <el-table :data="students" height="550" border style="width: 100%" highlight-current-row @current-change="handleCurrentChange">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="sno" label="学号" width="180"></el-table-column>
      <el-table-column prop="sname" label="姓名" width="180"></el-table-column>
      <el-table-column prop="ssex" label="性别" width="180"></el-table-column>
      <el-table-column prop="sage" label="年龄" width="180"></el-table-column>
      <el-table-column prop="sdep" label="院系" width="180"></el-table-column>
      <el-table-column prop="screate" label="入校时间" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot="header" slot-scope="scope">
          <el-button @click="handleAdd" type="primary" size="mini">添加</el-button>
        </template>
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.$index, scope.row)" type="primary" size="mini">编辑</el-button>
          <el-button @click="handleDelete(scope.$index, scope.row)" type="danger" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer title="我嵌套了 Form !" :before-close="handleClose" :visible.sync="dialog" direction="ltr" custom-class="demo-drawer" ref="drawer">
      <div class="demo-drawer__content">
        <el-form :model="form">
          <el-form-item label="活动名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="活动区域" :label-width="formLabelWidth">
            <el-select v-model="form.region" placeholder="请选择活动区域">
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="dialog = false">取 消</el-button>
          <el-button type="primary" @click="$refs.drawer.closeDrawer()" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'StudentComponent',
  data () {
    return {
      table: false,
      dialog: false,
      loading: false,
      form: {
        name: '',
        region: ''
      },
      formLabelWidth: '80px',
      students: []
    }
  },
  mounted () {
    this.$get('/student/list')
      .then(response => {
        this.students = response.data
      }).catch(() => {
        this.$message.error('Can not get all student!')
      })
  },
  methods: {
    handleCurrentChange (row) {
      // console.log(JSON.stringify(row))
    },
    handleAdd () {
      this.dialog = true
    },
    handleEdit (index, row) {
      this.$message.info(index + JSON.stringify(row))
    },
    handleDelete (index, row) {
      this.$message.success(index + JSON.stringify(row))
    },
    handleClose (done) {
      this.$confirm('确定要提交表单吗？')
        .then(_ => {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            done()
          }, 2000)
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped>

</style>
